package com.exprecards.common.web;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import com.exprecards.common.enums.ETipoUsuario;
import com.exprecards.common.persistence.DAOFactory;
import com.exprecards.common.persistence.dto.ExpTbSucursal;
import com.exprecards.common.persistence.dto.ExpTbSucursalPk;
import com.exprecards.common.persistence.exceptions.ExpTbCentroCostoDaoException;
import com.exprecards.common.persistence.exceptions.ExpTbPgDaoException;
import com.exprecards.common.persistence.exceptions.ExpTbSucursalDaoException;
import com.exprecards.common.persistence.exceptions.ExpTbUsuarioDaoException;
import com.exprecards.common.util.WebUtils;

@ManagedBean(name = "crudSucursalBean")
@ViewScoped
public class CRUDSucursalBean extends GenericCrudBean<ExpTbSucursal> {

	private static final long serialVersionUID = 1L;

	@ManagedProperty("#{userSessionBean}")
	private UserSessionBean userSessionBean;

	private int tipoUsuario;

	private boolean showPopupPanelDuplicados = true;

	private List<SelectItem> usuariosList = null;

	private List<SelectItem> ccList = null;

	public boolean isShowPopupPanelDuplicados() {
		return showPopupPanelDuplicados;
	}

	public void setShowPopupPanelDuplicados(boolean showPopupPanelDuplicados) {
		this.showPopupPanelDuplicados = showPopupPanelDuplicados;
	}

	public CRUDSucursalBean() throws ExpTbSucursalDaoException,
			ExpTbUsuarioDaoException, ExpTbPgDaoException,
			ExpTbCentroCostoDaoException {
		super();

		FacesContext facesContext = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) facesContext.getExternalContext()
				.getSession(false);

		userSessionBean = (UserSessionBean) session
				.getAttribute("userSessionBean");

		this.lista = consultar();
		this.listaSimple = consultarSimple();

		// listas
		usuariosList = WebUtils
				.createSelectItem(
						DAOFactory
								.getExpTbUsuarioDao()
								.findWherePTipoUsuarioEquals(
										DAOFactory
												.getExpTbPgDao()
												.findWhereCodigoExternoEquals(
														ETipoUsuario.EJECUTIVO_DE_CUENTA
																.getValue())[0]
												.getId()), false);

		ccList = WebUtils.createSelectItem(DAOFactory.getExpTbCentroCostoDao()
				.findAll(), false);
	}

	@Override
	protected boolean create() throws Exception {

		boolean result = false;

		this.valorActual.setEstado("A");

		DAOFactory.getExpTbSucursalDao().insert(this.valorActual);

		result = true;

		return result;
	}

	@Override
	protected boolean update() throws Exception {

		boolean result = false;

		DAOFactory.getExpTbSucursalDao()
				.update(new ExpTbSucursalPk(this.valorActual.getId()),
						this.valorActual);

		return result;
	}

	@Override
	protected void editable(int id) throws Exception {
		valorActual = DAOFactory.getExpTbSucursalDao().findByPrimaryKey(id);
	}

	@Override
	protected void delete(int id) throws Exception {
		DAOFactory.getExpTbSucursalDao().delete(new ExpTbSucursalPk(id));
	}

	@Override
	protected List<Map<String, String>> consultar() {

		return null;
	}

	@Override
	protected ExpTbSucursal[] consultarSimple()
			throws ExpTbSucursalDaoException {
		return DAOFactory.getExpTbSucursalDao().findAll();
	}

	@Override
	protected ExpTbSucursal createNew() {
		return new ExpTbSucursal();
	}

	@Override
	protected boolean isId() {
		boolean result = false;
		if (valorActual.getId() == 0) {
			result = false;
		} else {
			result = true;
		}
		return result;
	}

	@Override
	protected void clean() {

	}

	/**
	 * @return the userSessionBean
	 */
	public UserSessionBean getUserSessionBean() {
		return userSessionBean;
	}

	/**
	 * @param userSessionBean
	 *            the userSessionBean to set
	 */
	public void setUserSessionBean(UserSessionBean userSessionBean) {
		this.userSessionBean = userSessionBean;
	}

	public int getTipoUsuario() {
		return tipoUsuario;
	}

	public void setTipoUsuario(int tipoUsuario) {
		this.tipoUsuario = tipoUsuario;
	}

	public List<SelectItem> getUsuariosList() {
		return usuariosList;
	}

	public void setUsuariosList(List<SelectItem> usuariosList) {
		this.usuariosList = usuariosList;
	}

	public List<SelectItem> getCcList() {
		return ccList;
	}

	public void setCcList(List<SelectItem> ccList) {
		this.ccList = ccList;
	}

}
